package Sort1;

import java.util.PriorityQueue;

public class Sort {
    // 插入排序
    public static void insertSort(int[] array) {
        int i =1;
        int tmp = 0;
        for (; i < array.length; i++) {
            tmp = array[i];
            int j = i-1;
            for (; j>=0; j--){
                if (array[j] > tmp){
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    //选择排序
    public static void selectSort(int[] array){
        for (int left = 0; left <array.length ; left++) {
            int min = left;
            for (int i = left+1; i < array.length; i++) {
                if (array[i] < array[min]){
                    min = i;
                }
            }
            int tmp = array[min];
            array[min] = array[left];
            array[left] = tmp;
        }
    }



    //堆排序
    public static void heapSort(int[] array){
        PriorityQueue p = new PriorityQueue<Integer>(new IntCmp());

        int len = array.length-1;
        for (int i = 0; i < array.length; i++) {
            p.offer(array[i]);
        }
        for (int i = 0; i < array.length; i++) {
            array[i] = (int)p.poll();
        }
        for (int i = len; i > 0 ; i--) {
            int parent = 0;
            int tmp = array[i];
            array[i] = array[0];
            array[0] = tmp;
            len--;
            //向下调整

            int child = (2*parent)+1;//左孩子下标
            while (child < len+1) {
                if(child+1 < len+1 && array[child] < array[child+1]) {
                    child++;
                }
                //child一定是 左右孩子最大值的下标
                if(array[child] > array[parent]) {
                    tmp = array[child];
                    array[child] = array[parent];
                    array[parent] = tmp;
                    parent = child;
                    child = 2*parent+1;
                }else {
                    //已经是大根堆了
                    break;
                }
            }
        }

    }

    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            boolean flag = false;
            for (int j = i+1; j < array.length; j++) {
                if (array[i] > array[j]){
                    int tmp = array[i];
                    array[i] = array[j];
                    array[j] = tmp;
                    flag = true;
                }
            }
            if (!flag){
                return;
            }
        }
    }



    public static void bubbleSort2(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j] > array[j+1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flag = true;
                }
            }
            if (!flag){
                return;
            }
        }
    }


}